package com.flying.watch2;

import org.apache.zookeeper.ZooKeeper;

import java.util.List;

public class SubClient2_2 {
    static ZooKeeper zk;
    static String server = "/servers";

    public static void main(String[] args) throws Exception {
//        String hosts = "192.168.106.13:2181";
        String hosts = "127.0.0.1:2181";
        zk = new ZooKeeper(hosts, 30000, event -> {
            // 监听"/servers"节点下的子节点变化事件, 更新server列表, 并重新注册监听
            System.out.println("收到一条watch数据,类型:"+event.getType()+"path:"+event.getPath());
//            try {
//                List<String> subList = zk.getChildren(server, true);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
        });

//        byte[] bytes = zk.getData(server, true, null);
//        String str = new String(bytes);
//        System.out.println(str);
        List<String> subList = zk.getChildren(server, true);
        System.out.println(subList);
        Thread.sleep(Integer.MAX_VALUE);
    }


}